package cn.tedu.cwfw.dynamic.controller;

import cn.tedu.cwfw.base.response.JsonResult;
import cn.tedu.cwfw.dynamic.pojo.dto.DynamicQuery;
import cn.tedu.cwfw.dynamic.pojo.dto.SaveDynamicFrom;
import cn.tedu.cwfw.dynamic.pojo.vo.DynamicVO;
import cn.tedu.cwfw.dynamic.service.DynamicService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Slf4j
@RestController
@RequestMapping("/cwfw/cutecircle/dynamic")
public class DynamicController {

    @Autowired
    private DynamicService dynamicService;

    /**
     * 萌友圈首页的动态，
     * @param dynamicQuery 根据动态类型查询（推荐，关注）
     * @return List<DynamicVO>
     */
    @GetMapping("selectByType")
    public JsonResult selectByType(DynamicQuery dynamicQuery) {
        log.debug("查询首页动态请求:入参:dynamicQuery={}",dynamicQuery);
        List<DynamicVO> list = dynamicService.selectByType(dynamicQuery);
        return JsonResult.ok(list);
    }

    /**
     * 根据动态id查询动态详情
     * @param id 动态id
     * @return DynamicVO
     */
    @PostMapping("selectById/{id}")
    public JsonResult selectById(@PathVariable Long id) {
        log.debug("查询动态详情请求:入参:id={}",id);
        DynamicVO dynamicVO = dynamicService.selectDynamicById(id);
        log.debug("查询动态详情请求:入参:{},出参:{}",id,dynamicVO);
        return JsonResult.ok(dynamicVO);
    }


    /**
     * 点赞动态
     * @param id 动态id
     * @return JsonResult
     */
    @PostMapping("likeDynamic/{id}")
    public JsonResult likeDynamic(@PathVariable Long id) {
        log.debug("点赞动态请求:入参:id={}",id);
        dynamicService.likeDynamic(id);
        return JsonResult.ok();
    }

    /**
     * 添加动态
     * @param saveDynamicFrom 动态信息
     */
    @PostMapping("saveDynamic")
    public JsonResult saveDynamic(SaveDynamicFrom saveDynamicFrom) {
        log.debug("添加动态请求:入参{}",saveDynamicFrom);
        dynamicService.saveDynamic(saveDynamicFrom);
        return JsonResult.ok();
    }

    /**
     * 删除动态
     * @param id 动态id
     */
    @PostMapping("delete/{id}")
    public JsonResult delete(@PathVariable Long id) {
        log.debug("删除动态请求:入参:id={}",id);
        dynamicService.delete(id);
        return JsonResult.ok();
    }

}
